package com.vf.admin.client.tx.utils;

import com.alibaba.fastjson.JSON;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.trtc.v20190722.TrtcClient;
import com.tencentcloudapi.trtc.v20190722.models.*;
import com.vf.admin.common.Constants;
import com.vf.admin.client.tx.dto.trtc.TxyResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static com.vf.admin.client.tx.utils.txConfig.SDKAPPID;
import static com.vf.admin.client.tx.utils.txConfig.SECRETID;
import static com.vf.admin.client.tx.utils.txConfig.SECRETKEY;

/**
 * 腾讯云实时音视频接口
 */
public class TxTrtc {

    //腾讯云实时音视频参数
    public static String trtcUrl = "trtc.tencentcloudapi.com";  //就近地域接入域名URL
    public static String region = "ap-guangzhou";  //地域

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    public static TxTrtc getInstance(){
        return new TxTrtc();
    }

    /**
     * 把用户移出房间
     * @param userId  用户id(腾讯云对应的用户id)
     */
    public static boolean yiChuUser(long roomId, String[] userId){
        try{
            Credential cred = new Credential(SECRETID, SECRETKEY);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(trtcUrl);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            TrtcClient client = new TrtcClient(cred, region, clientProfile);

            RemoveUserRequest req = new RemoveUserRequest();
            req.setUserIds(userId);

            req.setSdkAppId(SDKAPPID);
            req.setRoomId(roomId);

            RemoveUserResponse resp = client.RemoveUser(req);
            String resultContent = RemoveUserResponse.toJsonString(resp);
            TxyResponse txyResponse = null;
            try{
                txyResponse = JSON.parseObject(resultContent, TxyResponse.class);
                System.out.println(txyResponse.getError());
                System.out.println(txyResponse.getRoomList());
                System.out.println(txyResponse.getTotal());
                System.out.println(txyResponse.getData());
                System.out.println(txyResponse.getRequestId());

            }catch(Exception e){
                System.out.println(txyResponse.getRequestId());
/*
                logger.info("类型转换一场异常，请检查类属性与数据表的属性是否一致:" + e);
*/
                throw new RuntimeException("类型转换一场异常，请检查类属性与数据表的属性是否一致:" + e);
            }
            return true;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            return false;
        }
    }


    /**
     * 解散房间
     * @param roomId  用户id(腾讯云对应的房间id)
     */
    public static boolean jieSanRoom(long roomId){

        try{
            Credential cred = new Credential(SECRETID, SECRETKEY);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(trtcUrl);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            TrtcClient client = new TrtcClient(cred,region , clientProfile);

            DismissRoomRequest req = new DismissRoomRequest();
            req.setSdkAppId(SDKAPPID);
            req.setRoomId(roomId);

            DismissRoomResponse resp = client.DismissRoom(req);

            String resultContent = DismissRoomResponse.toJsonString(resp);
            TxyResponse txyResponse = null;
            try{
                txyResponse = JSON.parseObject(resultContent, TxyResponse.class);
            }catch(Exception e){
                throw new RuntimeException("类型转换一场异常，请检查类属性与数据表的属性是否一致:" + e);
            }
            System.out.println(txyResponse.getRequestId());
            return true;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            return false;
        }
    }

    /**
     * 查询房间列表
     */
    private TxyResponse findRoomList(){
        try{
            Credential cred = new Credential(SECRETID, SECRETKEY);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(trtcUrl);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            TrtcClient client = new TrtcClient(cred, region, clientProfile);

            DescribeRoomInformationRequest req = new DescribeRoomInformationRequest();
            req.setSdkAppId(SDKAPPID+"");

            long nowTime = System.currentTimeMillis()/1000L;
            req.setStartTime(nowTime - 60);
            req.setEndTime(nowTime);
            DescribeRoomInformationResponse resp = client.DescribeRoomInformation(req);
            System.out.println(DescribeRoomInformationResponse.toJsonString(resp));
            String resultContent = DismissRoomResponse.toJsonString(resp);
            TxyResponse txyResponse = null;
            try{
                txyResponse = JSON.parseObject(resultContent, TxyResponse.class);
                System.out.println(txyResponse.getTotal());
                System.out.println(txyResponse.getRoomList());
            }catch(Exception e){
                logger.info("类型转换一场异常，请检查类属性与数据表的属性是否一致:" + e);
                throw new RuntimeException("类型转换一场异常，请检查类属性与数据表的属性是否一致:" + e);
            }
            return txyResponse;
        }catch(Exception e){
            System.out.println(e.toString());
        }
        return null;
    }

    /**
     * 查询当前在线的房间总数量和当前在线的用户总数量
     */
    private TxyResponse findNowRoomSize(){
        try{
            Credential cred = new Credential(SECRETID, SECRETKEY);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(trtcUrl);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            TrtcClient client = new TrtcClient(cred, region, clientProfile);

            DescribeRealtimeScaleRequest req = new DescribeRealtimeScaleRequest();
            String[] dataType = {"RoomNum", "UserNum"};
            req.setDataType(dataType);

            long nowTime = System.currentTimeMillis()/1000L;

            req.setEndTime(nowTime);
            req.setStartTime(nowTime - 60);
            req.setSdkAppId(""+SDKAPPID);

            DescribeRealtimeScaleResponse resp = client.DescribeRealtimeScale(req);
            System.out.println(DescribeRealtimeScaleResponse.toJsonString(resp));

            String resultContent = DismissRoomResponse.toJsonString(resp);
            TxyResponse txyResponse = null;
            try{
                txyResponse = JSON.parseObject(resultContent, TxyResponse.class);
            }catch(Exception e){
                logger.info("类型转换一场异常，请检查类属性与数据表的属性是否一致:" + e);
                throw new RuntimeException("类型转换一场异常，请检查类属性与数据表的属性是否一致:" + e);
            }
            return txyResponse;
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return null;
    }


    public static void main(String[] args) throws Exception{
        try{

            //腾讯云服务端 实时音视频解散房间 把用于移除房间

            String[] userIds = {"123456789"};
            boolean flag=yiChuUser(123456792,userIds);
            System.out.println("-----------------:"+flag);
            //boolean flag=jieSanRoom(15981291);
            //System.out.println(flag);
            System.out.println("-----------------");
            //TxTrtc.getInstance().findRoomList();

//            TxyResponse txyResponse = DescribeInstances.getInstance().findRoomList();
//            System.out.println(txyResponse);


//            long nowTime = System.currentTimeMillis()/1000L;
//            System.out.println(nowTime+" "+(nowTime - 160));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
